﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DataAccess;
using DataAccess.Dal;
using DataAccess.DBConnections;
using DataAccess.ProxyConnections;
using Oracle.DataAccess.Client;

namespace Web.Business
{
    public partial class TokenBal
    {
        #region | Conts |

        public static DataAccessClient dal;//= new DataAccessClient(DATABASE_PROXY.AUTH);

        private static DBConnectionsEngine dbEngine;

        private static OracleConnection Conn;

        private static Hashtable hashtable;

        private static TokenBal _dal;

        private System.String sql = "";

        public static TokenBal GetInstance()
        {
            /* for Init */
            dal = new DataAccessClient(DATABASE_PROXY.AUTH);

            dbEngine = new DBConnectionsEngine();

            Conn = new DatabaseProxy().GetAuthConnection();

            _dal = new TokenBal();

            return _dal;
        }

        #endregion

        #region | Helper |

        internal bool Insert(p_APPTOKEN obj, out string err)
        {
            bool result = false;
            if (dal.Insert(obj, out err) == DB_RESULT.INCOMPLETED)result = true;

            return result;
        }

        internal bool Delete(p_APPTOKEN sender, out string err)
        {
            bool result = false;
            p_APPTOKEN obj = new p_APPTOKEN();
            obj.setCriteria(p_APPTOKEN_FIELD.APPTOKEN,CRITERIA_FLAG.Equal, sender.APPTOKEN);
            if (dal.Delete(obj, out err) == DB_RESULT.INCOMPLETED)result = true;

            return result;
        }
        #endregion

        #region | dbEngine |

        public IEnumerable<p_APPTOKEN> Select(System.Web.UI.Page page, p_APPTOKEN obj)
        {
            IEnumerable<p_APPTOKEN> entities = new List<p_APPTOKEN>();
            try
            {
                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" WHERE 1=1 ");

                hashtable = new Hashtable();

                if (obj != null)
                {
                    if (!String.IsNullOrEmpty(obj.DIVISION))
                    {
                        concast.Append(" AND DIVISION=:DIVISION ");
                        hashtable.Add(":DIVISION", obj.DIVISION);
                    }

                    if (!String.IsNullOrEmpty(obj.COMPANY_CODE))
                    {
                        concast.Append(" AND COMPANY_CODE=:COMPANY_CODE ");
                        hashtable.Add(":COMPANY_CODE", obj.COMPANY_CODE);
                    }

                    if (!String.IsNullOrEmpty(obj.FUNCNO))
                    {
                        concast.Append(" AND FUNCNO=:FUNCNO ");
                        hashtable.Add(":FUNCNO", obj.FUNCNO);
                    }

                    if (!Microsoft.VisualBasic.Information.IsNothing(obj.APPTOKEN))
                    {
                        concast.Append(" AND APPTOKEN=:APPTOKEN ");
                        hashtable.Add(":APPTOKEN", obj.APPTOKEN);
                    }

                    if (!String.IsNullOrEmpty(obj.TOKEN))
                    {
                        concast.Append(" AND TOKEN=:TOKEN ");
                        hashtable.Add(":TOKEN", obj.TOKEN);
                    }

                    if (!String.IsNullOrEmpty(obj.APPLNO))
                    {
                        concast.Append(" AND APPLNO=:APPLNO ");
                        hashtable.Add(":APPLNO", obj.APPLNO);
                    }

                    if (!String.IsNullOrEmpty(obj.APPLNAME))
                    {
                        concast.Append(" AND APPLNAME=:APPLNAME ");
                        hashtable.Add(":APPLNAME", obj.APPLNAME);
                    }

                    if (!String.IsNullOrEmpty(obj.EMPNO))
                    {
                        concast.Append(" AND EMPNO=:EMPNO ");
                        hashtable.Add(":EMPNO", obj.EMPNO);
                    }

                    if (!String.IsNullOrEmpty(obj.PASSWORD))
                    {
                        concast.Append(" AND PASSWORD=:PASSWORD ");
                        hashtable.Add(":PASSWORD", obj.PASSWORD);
                    }

                    if (!Microsoft.VisualBasic.Information.IsDate(obj.CRDATE))
                    {
                        concast.Append(" AND CRDATE=:CRDATE ");
                        hashtable.Add(":CRDATE", obj.CRDATE);
                    }

                    if (!String.IsNullOrEmpty(obj.LANG))
                    {
                        concast.Append(" AND LANG=:LANG ");
                        hashtable.Add(":LANG", obj.LANG);
                    }

                    if (!String.IsNullOrEmpty(obj.FUNCTION_GROUP))
                    {
                        concast.Append(" AND FUNCTION_GROUP=:FUNCTION_GROUP ");
                        hashtable.Add(":FUNCTION_GROUP", obj.FUNCTION_GROUP);
                    }

                }
                sql = System.String.Format(@" SELECT DIVISION ,COMPANY_CODE ,FUNCNO ,APPTOKEN ,TOKEN ,APPLNO ,APPLNAME ,EMPNO ,PASSWORD ,CRDATE ,LANG ,FUNCTION_GROUP  FROM APPTOKEN");

                sql = sql + concast.ToString();

                entities = dbEngine.ToList<p_APPTOKEN>(sql, hashtable, Conn).AsParallel();

            }
            catch (System.Exception ex)
            {
                Core.Modal.Exception(
                    page: page,
                    message: Core.Exception.ModalException(
                                        method: System.Reflection.MethodBase.GetCurrentMethod(),
                                        ex: ex));
                entities = null;
            }
            return entities;
        }

        #endregion
    }
}